{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58d34b26-aa48-40c2-af1a-9415dfa32467",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db1e3a2f-a3c9-485a-8b38-ebcfcd09c441",
   "metadata": {},
   "source": [
    "# Images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f4b6a86-2e60-4a55-bf5c-99f1f0f6d409",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install sliceguard[all]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e709a816-141d-486d-aea6-bf4e416ca1af",
   "metadata": {},
   "outputs": [],
   "source": [
    "from renumics import spotlight\n",
    "from sliceguard import SliceGuard\n",
    "from sliceguard.data import from_huggingface\n",
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "235e85d5-8915-437c-94b4-4014bdecb1b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = from_huggingface(\"Matthijs/snacks\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4f28295-faad-4f11-bb1e-70d47b239713",
   "metadata": {},
   "outputs": [],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "935d56c2-e6be-4916-bcef-f2a09f15ce19",
   "metadata": {},
   "outputs": [],
   "source": [
    "sg = SliceGuard()\n",
    "issues = sg.find_issues(df.sample(500), features=[\"image\"], y=\"label\", metric=accuracy_score)\n",
    "report_df, spotlight_data_issues, spotlight_dtypes, spotlight_layout = sg.report(no_browser=True)\n",
    "spotlight.show(report_df, dtype=spotlight_dtypes, issues=spotlight_data_issues, layout=spotlight_layout)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fb3f8b7-0b51-44c1-915f-af8c00effa2a",
   "metadata": {},
   "source": [
    "# Audio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68f159aa-a8b1-480e-aef1-e19ff17d09b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = from_huggingface(\"renumics/emodb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f294d7f5-12ad-4f63-a68e-b46a386cd45e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61b061f7-c3f4-4004-a200-b00cf13a37c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "sg = SliceGuard()\n",
    "issues = sg.find_issues(df, features=[\"audio\", \"gender\"], y=\"emotion\", metric=accuracy_score)\n",
    "report_df, spotlight_data_issues, spotlight_dtypes, spotlight_layout = sg.report(no_browser=True)\n",
    "spotlight.show(report_df, dtype=spotlight_dtypes, issues=spotlight_data_issues, layout=spotlight_layout)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f49eb41c-8882-49cc-8e59-d62bf4f7a751",
   "metadata": {},
   "source": [
    " # Text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd7154cc-97fa-4deb-9e28-65864cdd195f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = from_huggingface(\"dair-ai/emotion\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ec8ea94-2799-431f-9c4f-cd7cab6ac3b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09e525be-0a28-4558-a3e6-2d293a7bdd7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "issues = sg.find_issues(df, features=[\"text\"], y=\"label\", metric=accuracy_score, min_support=5, min_drop=0.3, n_slices=20)\n",
    "report_df, spotlight_data_issues, spotlight_dtypes, spotlight_layout = sg.report(no_browser=True)\n",
    "spotlight.show(report_df, dtype=spotlight_dtypes, issues=spotlight_data_issues, layout=spotlight_layout)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8df0c28-1907-4628-8a26-a5480b56aca5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
